package order

import "time"

// ConcreteOrderBuilder 具体建造者
type ConcreteOrderBuilder struct {
	order *Order
}

func NewConcreteOrderBuilder() *ConcreteOrderBuilder {
	return &ConcreteOrderBuilder{order: &Order{
		CreateAt: time.Now(),
		Items:    []OrderItem{},
	}}
}

func (r *ConcreteOrderBuilder) SetOrderId(orderId string) OrderBuilder {
	r.order.ID = orderId
	return r
}

func (r *ConcreteOrderBuilder) SetCustomer(customer Customer) OrderBuilder {
	r.order.Customer = customer
	return r
}

func (r *ConcreteOrderBuilder) AddItem(item OrderItem) OrderBuilder {
	r.order.Items = append(r.order.Items, item)
	return r
}

func (r *ConcreteOrderBuilder) Build() *Order {
	r.order.CalculateTotal()
	return r.order
}

func (r *ConcreteOrderBuilder) Reset() {
	r.order = &Order{
		CreateAt: time.Now(),
		Items:    []OrderItem{},
	}
}
